package com.hex.ds.hdrs.common.helper;

import cn.hutool.core.collection.CollectionUtil;
import com.hex.ds.hdrs.common.utils.HdrsDateUtil;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.*;

/**
 * @author lwz
 * @date 2020-12-30
 * @des
 */
public class EchartsHelper {

    /**
     * 获取Echart时间横坐标
     *
     * @param endDate  截止时间
     * @param duration 时长（分钟）
     * @param interval 时间间隔 （分钟）
     * @param offset   补偿量（分钟）
     * @param patten   生成的时间格式
     * @return
     */
    public static List<String> getXAxisTime(Date endDate, Integer duration, Integer interval, Integer offset, String patten) {
        List<String> xAxisTimeList = new ArrayList<>();
        Integer count = duration / interval;
        endDate = DateUtils.addMinutes(endDate, offset);
        for (int i = count; i >= 0; i--) {
            String time = HdrsDateUtil.format(DateUtils.addMinutes(endDate, -interval * i), patten);
            xAxisTimeList.add(time);
        }
        return xAxisTimeList;
    }

    public static List<Integer> getSeriesData(List<Map> seriesDataList, List<String> xAxisTimeList, String filterLable, String filterValue) {
        return getSeriesData(seriesDataList, xAxisTimeList, filterLable, filterValue, "count");
    }

    /**
     * 获取Echart纵坐标数据值，时间点没有数据的补0
     *
     * @param seriesDataList 纵坐标初始数据
     * @param xAxisTimeList  横坐标时间数据
     * @param filterLable    纵坐标初始数据，过滤标识
     * @param filterValue    纵坐标初始数据，过滤值
     * @return
     */
    public static List<Integer> getSeriesData(List<Map> seriesDataList, List<String> xAxisTimeList, String filterLable, String filterValue, String value) {
        //1、将横坐标时间List转成Map，初始化每个时间点数值作为纵坐标初始化值
        Map<String, Integer> xAxisTimeMap = new HashMap();
        for (String time : xAxisTimeList) {
            xAxisTimeMap.put(time, 0);
        }
        //2、遍历纵坐标数据值，更新纵坐标初始化值
        if (!CollectionUtil.isEmpty(seriesDataList)) {
            for (Map map : seriesDataList) {
                if (StringUtils.isNotBlank(filterLable) && StringUtils.isNotBlank(filterValue)) {
                    String dataValue = "";
                    dataValue = (String) map.get(filterLable);
                    if (StringUtils.isBlank(dataValue)) {
                        dataValue = (String) map.get(filterLable.toUpperCase());
                    }
                    if (filterValue.equals(dataValue)) {
                        String time = (String) map.get("time") == null ? (String) map.get("TIME") : (String) map.get("time");
                        Integer count = 0;
                        if (map.containsKey(value)) {
                            count = MapUtils.getInteger(map, value);
                        } else if (map.containsKey(value.toUpperCase())) {
                            count = MapUtils.getInteger(map, value.toUpperCase());
                        }
                        xAxisTimeMap.put(time, count);
                    }
                }
            }
        }
        //3、将纵坐标带有时间的map转成List
        List<Integer> seriesDataResult = new ArrayList<>();
        for (String time : xAxisTimeList) {
            seriesDataResult.add(xAxisTimeMap.get(time));
        }
        return seriesDataResult;
    }

    public static Map getSeriesData(String name, List data, String type) {
        Map map = new HashMap();
        map.put("name", name);
        map.put("animationDuration", 2800);
        map.put("animationEasing", "quadraticOut");
        map.put("type", type);
        map.put("data", data);
        return map;
    }

    /**
     * 获取不同时间段的时间横坐标
     *
     * @param nowDate  当前时间
     * @param timeType 类型 1: 30分钟、2: 1小时、3: 6小时 4: 12小时 5: 1天 6: 7天 7: 30天
     * @return
     */
    public static List<String> getTimes(Date nowDate, String timeType) {
        switch (timeType) {
            case "1":
                return EchartsHelper.getXAxisTime(nowDate, 30, 1, 0, "HH:mm");
            case "2":
                return EchartsHelper.getXAxisTime(nowDate, 60, 1, 0, "HH:mm");
            case "3":
                return EchartsHelper.getXAxisTime(nowDate, 60 * 6, 1, 0, "HH:mm");
            case "4":
                return EchartsHelper.getXAxisTime(nowDate, 60 * 12, 60, 0, "HH");
            case "5":
                return EchartsHelper.getXAxisTime(nowDate, 60 * 24, 60, 0, "HH");
            case "6":
                return EchartsHelper.getXAxisTime(nowDate, 60 * 24 * 7, 60 * 24, 0, "MM-dd");
            case "7":
                return EchartsHelper.getXAxisTime(nowDate, 60 * 24 * 30, 60 * 24, 0, "MM-dd");
            default:
                return EchartsHelper.getXAxisTime(nowDate, 30, 1, 0, "HH:mm");
        }
    }

    public static String getStartTime(Date nowDate, String timeType) {
        switch (timeType) {
            case "1":
                return HdrsDateUtil.format(DateUtils.addMinutes(nowDate, -30), "yyyy-MM-dd HH:mm");
            case "2":
                return HdrsDateUtil.format(DateUtils.addMinutes(nowDate, -60), "yyyy-MM-dd HH:mm");
            case "3":
                return HdrsDateUtil.format(DateUtils.addHours(nowDate, -6), "yyyy-MM-dd HH:mm");
            case "4":
                return HdrsDateUtil.format(DateUtils.addHours(nowDate, -12), "yyyy-MM-dd HH");
            case "5":
                return HdrsDateUtil.format(DateUtils.addHours(nowDate, -24), "yyyy-MM-dd HH");
            case "6":
                return HdrsDateUtil.format(DateUtils.addDays(nowDate, -7), "yyyy-MM-dd");
            case "7":
                return HdrsDateUtil.format(DateUtils.addDays(nowDate, -30), "yyyy-MM-dd");
            default:
                return HdrsDateUtil.format(DateUtils.addMinutes(nowDate, -30), "yyyy-MM-dd HH:mm");
        }
    }
}
